package com.midea.iot;

import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.mapreduce.Reducer;

import java.io.IOException;

/**
 * SortReducer类的输出
 *  key设计为LongWritable作为排名，
 *  value设计为LongWritable作为数据源中的数字
 */
public class SortReducer extends Reducer<LongWritable, NullWritable, LongWritable, LongWritable> {

    //longWritable 作为排名
    long index = 0;
    LongWritable resultKey = new LongWritable();
    @Override
    protected void reduce(LongWritable mapperKey, Iterable<NullWritable> values, Context context) throws IOException, InterruptedException {

        // 这里需要对values进行排序，是因为数据源中可能存在相等的数字
        for (NullWritable value: values) {
            index++;
            resultKey.set(index);
            context.write(resultKey, mapperKey);
        }
    }
}
